package cn.woniu.ecommerceplatform.service.impl;

import cn.woniu.ecommerceplatform.entity.Orders;
import cn.woniu.ecommerceplatform.entity.Services;
import cn.woniu.ecommerceplatform.mapper.ServicesMapper;
import cn.woniu.ecommerceplatform.service.ServicesService;
import cn.woniu.ecommerceplatform.util.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ServicesServiceImpl implements ServicesService {

    @Autowired(required = false)
    private ServicesMapper servicesMapper;



    @Override
    public ResponseResult<?> servicesList(String goodName, String orderNum, Integer status, String account, Integer pageNum, Integer pageSize) {
        try {
            List<Services> services;
            PageInfo<Services> servicesPageInfo;
            if(pageNum!=null && pageSize!=null) {
                PageHelper.startPage(pageNum, pageSize);
                services = servicesMapper.servicesList(goodName, orderNum, status, account);
                servicesPageInfo = new PageInfo<>(services);
                if (!(servicesPageInfo.getSize() > 0) && pageNum > 1) {
                    PageHelper.startPage(pageNum - 1, pageSize);
                    services = servicesMapper.servicesList(goodName, orderNum, status, account);
                    servicesPageInfo = new PageInfo<>(services);
                }
            }else {
                services=servicesMapper.servicesList(goodName, orderNum, status, account);
                servicesPageInfo=new PageInfo<>(services);
            }
            if(services.isEmpty()){
                return new ResponseResult<>().isNull();
            }else {
                return new ResponseResult<>().ok(servicesPageInfo);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResponseResult<?> servicesDelete(Integer id) {
            int count = servicesMapper.servicesDelete(id);
            if(count>0){
                return new ResponseResult<>().ok();
            }else{
                return new ResponseResult<>().notok();
            }
    }

    @Override
    public ResponseResult<?> servicesDetails(Integer serviceId) {
        Services services = servicesMapper.servicesDetails(serviceId);
        if(services != null){
            return new ResponseResult<>().ok(services);
        }else{
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> serviceUpdate(Services sales) {
        int count = servicesMapper.serviceUpdate(sales);
        if(count>0){
            return new ResponseResult<>().ok();
        }else{
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> servicesDetailsUpdate(Services sales) {
        sales.setSalesTime(new Date());
        int count = servicesMapper.servicesDetailsUpdate(sales);
        if(count>0){
            return new ResponseResult<>().ok();
        }else{
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> userServicesDelete(Integer serviceId) {
        int count = servicesMapper.userServicesDelete(serviceId);
        if(count>0){
            return new ResponseResult<>().ok();
        }else{
            return new ResponseResult<>().notok();
        }
    }

    @Override
    @Transactional
    public ResponseResult<?> servicesAdd(Services sales) {
        //sales.setEndTime(new Date());
        int count = servicesMapper.servicesAdd(sales);
        if(count>0){
            return new ResponseResult<>().ok();
        }else{
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> queryServicesByOrderId(Integer orderId) {
        Services services = servicesMapper.queryServicesByOrderId(orderId);
        if(services != null){
            return new ResponseResult<>().ok(services);
        }else{
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> serviceDealAdd(Services sales) {
        sales.setSalesTime(new Date());
        int count = servicesMapper.serviceDealAdd(sales);
        if(count>0){
            return new ResponseResult<>().ok();
        }else{
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> serviceUpdateByOrderId(Services sales) {
        int count = servicesMapper.serviceUpdateByOrderId(sales);
        if(count>0){
            return new ResponseResult<>().ok();
        }else{
            return new ResponseResult<>().notok();
        }
    }
}
